package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserPartyFiles;
import com.ruoyi.system.service.IUserPartyFilesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 党员发展文件Controller
 * 
 * @author ruoyi
 * @date 2024-07-25
 */
@RestController
@RequestMapping("/system/files")
public class UserPartyFilesController extends BaseController
{
    @Autowired
    private IUserPartyFilesService userPartyFilesService;

    /**
     * 查询党员发展文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:files:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserPartyFiles userPartyFiles)
    {
        startPage();
        List<UserPartyFiles> list = userPartyFilesService.selectUserPartyFilesList(userPartyFiles);
        return getDataTable(list);
    }

    /**
     * 导出党员发展文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:files:export')")
    @Log(title = "党员发展文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserPartyFiles userPartyFiles)
    {
        List<UserPartyFiles> list = userPartyFilesService.selectUserPartyFilesList(userPartyFiles);
        ExcelUtil<UserPartyFiles> util = new ExcelUtil<UserPartyFiles>(UserPartyFiles.class);
        util.exportExcel(response, list, "党员发展文件数据");
    }

    /**
     * 获取党员发展文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:files:list')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(userPartyFilesService.selectUserPartyFilesByUserId(recordId));
    }

    /**
     * 新增党员发展文件
     */
    @PreAuthorize("@ss.hasPermi('system:files:manage')")
    @Log(title = "党员发展文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserPartyFiles userPartyFiles)
    {
    	Long uuid = uuid();
    	userPartyFiles.setRecordId(uuid);
    	System.out.println("文件uuid："+userPartyFiles.getRecordId());
        return toAjax(userPartyFilesService.insertUserPartyFiles(userPartyFiles));
    }

    /**
     * 修改党员发展文件
     */
    @PreAuthorize("@ss.hasPermi('system:files:manage')")
    @Log(title = "党员发展文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserPartyFiles userPartyFiles)
    {
        return toAjax(userPartyFilesService.updateUserPartyFiles(userPartyFiles));
    }
    
    /**
     * 修改党员发展文件状态
     */
//    @PreAuthorize("@ss.hasPermi('system:files:manage')")
    @Log(title = "党员发展文件", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCheckStatus")
    public AjaxResult editCheckStatus(@RequestBody UserPartyFiles userPartyFiles)
    {
    	Long userId = userPartyFiles.getUserId();
    	Integer checkStatus = userPartyFiles.getCheckStatus();
    	// 使用增强型 for 循环
        for (String stage : userPartyFiles.getStages()) {
        	UserPartyFiles update = new UserPartyFiles();
        	update.setUserId(userId);
        	update.setStage(stage);
        	update.setCheckStatus(checkStatus);
        	userPartyFilesService.updateCheckStatus(update);
        }
//    	UserPartyFiles update = new UserPartyFiles();
//    	update.setUserId(userPartyFiles.getUserId());
//    	update.setStage(userPartyFiles.getStage());
//    	update.setCheckStatus(userPartyFiles.getCheckStatus());
//    	userPartyFilesService.updateCheckStatus(update);
        return toAjax(1);
    }

    /**
     * 删除党员发展文件
     */
    @PreAuthorize("@ss.hasPermi('system:files:manage')")
    @Log(title = "党员发展文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(userPartyFilesService.deleteUserPartyFilesByUserIds(userIds));
    }
    
    public Long uuid() {
    	UUID uuid = UUID.randomUUID();
        long id = Math.abs(uuid.getMostSignificantBits()) % 100000000000000L;
        return id;
    }
}
